/*
 *  Compound.cpp
 *  CompoundParser
 *
 *  Created by Ryan Edwards-Crewe on 09-01-10.
 *  Copyright 2010 RECCoder. All rights reserved.
 *
 */

#include <list>

#include "Compound.h"

Compound::Compound() {};

Compound::~Compound() {};

int Compound::addMoleData(char& ch, FILE* fptr) {
	int verify;
	
	verify = fscanf(fptr, "%d%d%d%d%d%d%s%s", 
					&CID, 
					&ATOM_COUNT, 
					&BOND_COUNT, 
					&SUBSTRUCTURE_COUNT, 
					&FEATURE_COUNT, 
					&SET_COUNT,
					MOLECULE_TYPE,
					MOLECULE_CHARGE);

	if (verify < 8)
		return -1;
	else 
		return 0;
}

int Compound::addAtomData(char& ch, FILE* fPtr) {
	Atom A;
	A.setAtomData(ch, fPtr);
	AtomList.push_back(A);
	return 0;
}

int Compound::addBondData(char& ch, FILE* fPtr) {
	Bond B;
	B.setBondData(ch, fPtr);
	BondList.push_back(B);
	return 0;
}

int Compound::addSubsData(char& ch, FILE* fPtr) {
	Substructure S;
	S.setSubsData(ch, fPtr);
	SubsList.push_back(S);
	return 0;
}

int Compound::getAtomCount() {
	return ATOM_COUNT;
}

int Compound::getBondCount() {
	return BOND_COUNT;
}

int Compound::getSubsCount() {
	return SUBSTRUCTURE_COUNT;
}

int Compound::getCID() {
	return CID;
}

void Compound::clearCompound() {
	CID = 0;
	ATOM_COUNT = 0; 
	BOND_COUNT = 0;
	SUBSTRUCTURE_COUNT =0; 
	FEATURE_COUNT = 0;
	SET_COUNT = 0;
	
	for (int i = 0; i < 16; i++) {
		if (i < 12)
			MOLECULE_TYPE[i] = '\0';
		MOLECULE_CHARGE[i] = '\0';
	}
	
	AtomList.clear();
	BondList.clear();
	SubsList.clear();
}

void Compound::writeInsertStatement(FILE* fPtr) {
	
	fprintf(fPtr, "-- BEGIN COMPOUND: %d\n", getCID());
	fprintf(fPtr, "START TRANSACTION;\n");
	
	fprintf(fPtr, "INSERT INTO Molecule_Data (CID, ATOM_COUNT, BOND_COUNT, SUBSTRUCTURE_COUNT, FEATURE_COUNT, SET_COUNT, MOLECULE_TYPE, MOLECULE_CHARGE) VALUES\n(%d, %d, %d, %d, %d, %d, '%s', '%s');\n", 
			CID, 
			ATOM_COUNT, 
			BOND_COUNT, 
			SUBSTRUCTURE_COUNT, 
			FEATURE_COUNT, 
			SET_COUNT,
			MOLECULE_TYPE,
			MOLECULE_CHARGE);
	
	int counter=1;	
	fprintf(fPtr, "INSERT INTO Atom_Data (CID, ATOM_ID, ATOM, X_COORD, Y_COORD, Z_COORD, ATOM_TYPE, SUBSTRUCTURE_ID, SUBSTRUCTURE_NAME, CHARGE) VALUES\n");
	for (list<Atom>::iterator aitr = AtomList.begin(); aitr != AtomList.end(); aitr++) {
		aitr->writeAtomData(getCID(), fPtr);
		(counter++ == AtomList.size()) ? fprintf(fPtr, ";\n") : fprintf(fPtr, ",\n");
	}
	
	counter=1;
	fprintf(fPtr, "INSERT INTO Bond_Data (CID, BOND_ID, FIRST_ATOM, SECOND_ATOM, BOND_TYPE) VALUES\n");
	for (list<Bond>::iterator bitr = BondList.begin(); bitr != BondList.end(); bitr++) {
		bitr->writeBondData(getCID(), fPtr);
		(counter++ == BondList.size()) ? fprintf(fPtr, ";\n") : fprintf(fPtr, ",\n");
	}
	
	counter=1;
	fprintf(fPtr, "INSERT INTO Substructure_Data (CID, SUBSTRUCTURE_ID, SUBSTRUCTURE_NAME, ROOT_ATOM_ID, SUBSTRUCTURE_TYPE, DICTIONARY_TYPE, CHAIN, CHAIN_SUBTYPE, INTERSUBSTRUCTURE_BONDS) VALUES\n");
	for (list<Substructure>::iterator sitr = SubsList.begin(); sitr != SubsList.end(); sitr++) {
		sitr->writeSubsData(getCID(), fPtr);
		(counter++ == SubsList.size()) ? fprintf(fPtr, ";\n") : fprintf(fPtr, ",\n");
	}
	
	fprintf(fPtr, "COMMIT;\n");
	fprintf(fPtr, "-- END COMPOUND: %d\n\n", getCID());
}